package com.car.loan.project.cm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.loan.common.enums.SwitchStatus;
import com.car.loan.common.enums.UserStatus;
import com.car.loan.common.utils.BeanUtils;
import com.car.loan.common.utils.DateUtils;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.project.cm.domain.CmDealer;
import com.car.loan.project.cm.domain.CmInsuranceCompany;
import com.car.loan.project.cm.domain.CmSales;
import com.car.loan.project.cm.mapper.CmSalesMapper;
import com.car.loan.project.cm.service.ICmSalesService;
import com.car.loan.project.cm.util.select.SalesBaseDataSelect;
import com.car.loan.project.cm.util.vo.SelectedVo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 销货单位Service业务层处理
 *
 * @author ruoyi
 * @date 2019-10-19
 */
@Service
public class CmSalesServiceImpl implements ICmSalesService {

    @Autowired
    private CmSalesMapper cmSalesMapper;

    /**
     * 查询销货单位
     *
     * @param id 销货单位ID
     * @return 销货单位
     */
    @Override
    public CmSales selectCmSalesById(Long id) {
        return cmSalesMapper.selectById(id);
    }

    /**
     * 查询销货单位列表
     *
     * @param cmSales 销货单位
     * @return 销货单位
     */
    @Override
    public List<CmSales> selectCmSalesList(CmSales cmSales) {
        QueryWrapper<CmSales> queryWrapper = new QueryWrapper<>(cmSales);
        return cmSalesMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<CmSales> selectPage(IPage<CmSales> page, QueryWrapper<CmSales> queryWrapper) {
        return cmSalesMapper.selectPage(page, queryWrapper);
    }

    @Override
    public IPage<SelectedVo> selectedPage(SalesBaseDataSelect salesBaseDataSelect) {
        IPage<CmSales> page = new Page<>(salesBaseDataSelect.getPageNum(),
            salesBaseDataSelect.getPageSize());
        QueryWrapper<CmSales> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(salesBaseDataSelect.getName())) {
            queryWrapper.like("name", salesBaseDataSelect.getName());
        }
        queryWrapper.select("id,name,status");
        if (null == salesBaseDataSelect.getOf() || SwitchStatus.OPEN.status == salesBaseDataSelect
            .getOf()) {
            queryWrapper.eq("status", UserStatus.OK.getCode());
        } else {
            queryWrapper.in("status", UserStatus.OK.getCode(), UserStatus.DISABLE.getCode());
        }
        queryWrapper.orderByDesc("id");
        IPage<CmSales> dealerIPage = cmSalesMapper
            .selectPage(page, queryWrapper);
        List<CmSales> records = dealerIPage.getRecords();
        IPage<SelectedVo> result = new Page<>();
        BeanUtils.copyBeanProp(result, dealerIPage);
        List<SelectedVo> selectedVos = new ArrayList<>();
        if (StringUtils.isNotEmpty(records)) {
            records.forEach(cmDealer -> {
                selectedVos.add(new SelectedVo().setValue(String.valueOf(cmDealer.getId()))
                    .setLabel(cmDealer.getName()).setStatus(cmDealer.getStatus()));
            });
        }
        result.setRecords(selectedVos);
        return result;
    }

    @Override
    public boolean checkNameUnique(String name, Long id) {
        CmSales query = new CmSales();
        query.setName(name);
        QueryWrapper<CmSales> queryWrapper = new QueryWrapper<>(query);
        CmSales cmSales = cmSalesMapper.selectOne(queryWrapper);
        return cmSales == null || id.equals(cmSales.getId());
    }

    /**
     * 新增销货单位
     *
     * @param cmSales 销货单位
     * @return 结果
     */
    @Override
    public int insertCmSales(CmSales cmSales) {
        cmSales.setCreateTime(DateUtils.getNowDate());
        return cmSalesMapper.insert(cmSales);
    }

    /**
     * 修改销货单位
     *
     * @param cmSales 销货单位
     * @return 结果
     */
    @Override
    public int updateCmSales(CmSales cmSales) {
        cmSales.setCreateTime(DateUtils.getNowDate());
        return cmSalesMapper.updateById(cmSales);
    }

    /**
     * 删除销货单位对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCmSalesByIds(Collection<Long> ids) {
        return cmSalesMapper.deleteBatchIds(ids);
    }

    /**
     * 删除销货单位信息
     *
     * @param id 销货单位ID
     * @return 结果
     */
    @Override
    public int deleteCmSalesById(Long id) {
        return cmSalesMapper.deleteById(id);
    }
}
